//****************************************************************************
// File Name: WineryTour.cpp
// Wine Tours project
// Group #2: Allen McLennan, Ryan Neal, Mike Matz
// CS 1C
// April 28, 2010
// Implementation of WineryTour class
//****************************************************************************

#include "WineryTour.h"

WineryTour::WineryTour(): wineryList(), winesPurchased(),
							totalSpent(0.0), totalDistance(0.0) {}

//AddWineryToList
//Pushes an object onto the Winery list
//	that user will tour
void WineryTour::AddWineryToList(Winery wObject)
{
	wineryList.push_back(wObject);
}

//GetWineryList
//Returns list of wineries that the user will tour
vector<Winery> WineryTour::GetWineryList()
{
	return wineryList;
}


//GetSpent
//Returns the value totalSpent
float WineryTour::GetSpent()
{
	return totalSpent;
}

//SetSpent
//Changes the value in spent
void WineryTour::SetSpent(float amount)
{
	totalSpent += amount;
}

//PrintSales
//Outputs sales, totalSpent, and totalDistance

void WineryTour::PrintSales()
{
	cout << "\n\nYou went " << totalDistance << " miles.\n";

	cout << "Wines purchased: \n" << setfill ('-') << setw(40) << '-'
		 << setfill(' ') << endl;

	for (int i = 0; i < winesPurchased.size(); i++)
	{
		cout << winesPurchased[i].vintage << ' ' << left
			 << setfill('.')  << setw(25) << fixed << setprecision(2)
			 << winesPurchased[i].wineName << '$' << winesPurchased[i].price
			 << endl << setprecision(6) << setfill(' ');
	}

	cout << right << setfill ('-') << setw(40) << '-' << setfill(' ')
		 << "\n\n" << "You spent: $ " << fixed << setprecision(2)
		 << totalSpent << "\n\n" << setprecision(6);

}

WineryTour::~WineryTour()
{
	Clear(wineryList);
	Clear(winesPurchased);
}

//WineryListSize
//Returns number of wineries stored in wineryList
int WineryTour::WineryListSize()
{
	return wineryList.size();
}

//AddWine
//Adds a wine to the vector
void WineryTour::AddWine(Wine wPurchase)
{
	winesPurchased.push_back(wPurchase);
}

//TakeTour
//Walks the vector of wineries
//	Calls functions that allow user to purchase wines
void WineryTour::TakeTour()
{
	unsigned int i;	//LCV
	int choice;		//menu option to buy wine
	int qty;
	Wine purchase;	//Wine the user bought

	//Initialize i to 0
	i = 0;

	//Check if there is any trip specified
	if(!IsEmpty(wineryList))
	{
		//Update distance traveled
		totalDistance += wineryList[i].GetDistance();

		while(i < WineryListSize())
		{
			//Output current winery and option to exit
			//	This also acts as a menu
			cout << endl << "You are currently at: "
				 << wineryList[i].toString() << endl
				 << "The wines available to select from are: "
				 << endl << "0. I don't want to purchase any wine here."
				 << endl;

			//Outputs the wines
			wineryList[i].OutputWineList();

			do {
				cout << "Please enter the number of a wine to purchase"
					 << " or 0 to go to the next winery. ";
				cin >> choice;
				cin.ignore(100, '\n');
				if(choice >= 1 && choice < wineryList[i].GetWineNum() + 1)
				{
					cout << "How many bottles would you like to purchase: ";
					cin >> qty;
					cin.ignore(100, '\n');

					purchase = wineryList[i].GetWine(choice - 1);

					//for (int j = 0; j < qty; j++)
						AddWine(purchase);
					totalSpent += purchase.price * qty;
				}
				else
				{
					cout << "Not a valid choice. Please try again.\n\n";
				}
			} while (choice != 0);


			while(choice != 0)
			{
				//Get the wine that they chose from the array
				//	Put it on vector of wines purchased
				if(choice >= 1 && choice < wineryList[i].GetWineNum() + 1)
				{
					purchase = wineryList[i].GetWine(choice - 1);
					AddWine(purchase);
					totalSpent += purchase.price;
					cout << "You've purchased " << purchase.wineName
						 << ".\n";
				}
				else
				{
					cout << "Not a valid choice. Please try again.\n\n";
				}

				cout << "Please enter the number of a wine to purchase"
					 << " or 0 to go to the next winery. ";
				cin >> choice;
				cin.ignore(100, '\n');
			}

			//Update distance traveled
			if(WineryListSize() - i != 1)
			{
				totalDistance += wineryList[i].
								GetDistanceToWinery(wineryList[i+1].GetId());
			}

			//Update LCV
			i++;
		}

		PrintSales();
		Clear(winesPurchased);
		Clear(wineryList);
	}
	else
	{
		cout << "Error. No trip specified.\n\n";
	}
}
